/*
 * Copyright (c) 2010 CCX(China) Co.,Ltd. All Rights Reserved.
 *
 * This software is the confidential and proprietary information of
 * CCX(China) Co.,Ltd. ("Confidential Information").
 * It may not be copied or reproduced in any manner without the express 
 * written permission of CCX(China) Co.,Ltd.
 *
 * Author: zhnb
 * Date: 2011-7-14 下午07:41:27
 */
package event;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * 自定义事件源 Lv2
 * @author zhnb
 *
 */
public class MyEventSource implements Serializable {

    private static final long serialVersionUID = 5771708039811734370L;
    
    // 名字做为触发点
    private String name = "";
    
    public String getName() {
        return name;
    }
    
    /**
     * 名字修改时查看，是否有变更。有则广播事件。
     */
    public void setName(String name) {
        if (this.getName() != name) {
            MyEventObject myEventObject = new MyEventObject(this);
            this.broadcast(myEventObject);
            this.name = name;
        } else {
            System.out.println("SKIP: setName()! Because they have a same name.");
        }
    }
    
    // 事件监听者
    private List<MyListener> myListenerList = new ArrayList<MyListener>();
    
    /**
     * 添加事件监听者
     */
    public void addListener(MyListener myListener){
        myListenerList.add(myListener);
    }

    /**
     * 删除事件监听者
     */
    public void removeListener(MyListener myListener){
        myListenerList.remove(myListener);
    }
    
    /**
     * 广播消息触发事件
     */
    public void broadcast(MyEventObject myEventObject){
       for (MyListener myListener : myListenerList) {
           myListener.onChange(myEventObject);
       } 
    }
    
}
